package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.*;
import tech.aistar.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private ILandlordService landlordService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IRegisterService registerService;

    @Autowired
    private ISeeRoomTrendService seeRoomTrendService;
    @Autowired
    private IImageService iImageService;
    @Autowired
    private IHouseService iHouseService;
    @GetMapping("/Tenantmessage/{registerId}")
    public String selectTenantAll(HttpServletRequest request,@PathVariable("registerId") Integer registerId) {
//        String registerId = request.getParameter("registerId");
        Register register = registerService.selectById(Integer.valueOf(registerId));
        request.setAttribute("register",register);
        List<Message> messages = messageService.selectAll(Integer.valueOf(registerId));
//        List<Message> messages = messageService.selectAll(1);
        request.setAttribute("message", messages);
        Tenant tenant = tenantService.selectByregisterId(Integer.valueOf(registerId));
        request.setAttribute("tenant", tenant);
        List<Message> messageStates = messageService.selectByState(registerId);
        request.setAttribute("messageSize",messageStates.size());


        List<SeeRoomTrend> seeRoomTrends=seeRoomTrendService.selectThree();
        int i=0;
        for (SeeRoomTrend seeRoomTrend : seeRoomTrends) {
            i++;
            request.setAttribute("image"+i, iImageService.selectOneByHouseId(seeRoomTrend.getHouseId()));
            request.setAttribute("house"+i,iHouseService.selectByhouseId(seeRoomTrend.getHouseId()));
        }
        return "/personal_homepage/tenant/TenantCentre";
    }

    @PostMapping("/deltenant")
    public @ResponseBody
    Result deleteById(HttpServletRequest request,Integer id) {
        int count = messageService.deleteById(id);

        if (count == 1) {
            Result result = new Result("200", "删除成功");
            return result;
        } else {
            Result result = new Result("500", "删除失败");
            return result;
        }
    }




    @GetMapping("/landlordmessage/{registerId}")
    public String selectLandlordAll(HttpServletRequest request,@PathVariable("registerId") Integer registerId) {
//        String registerId = request.getParameter("registerId");
        List<Message> messages = messageService.selectAll(Integer.valueOf(registerId));
        request.setAttribute("message", messages);
        Landlord landlord = landlordService.selectByregisterId(Integer.valueOf(registerId));
        request.setAttribute("landlord", landlord);
        List<Message> messageStates = messageService.selectByState(registerId);
        request.setAttribute("messageSize",messageStates.size());

        return "/personal_homepage/landlord/news";
    }



    @GetMapping("/agentmessage/{registerId}")
    public String selectAgentAll(HttpServletRequest request,@PathVariable("registerId") Integer registerId) {
//        String registerId = request.getParameter("registerId");
        List<Message> messages = messageService.selectAll(Integer.valueOf(registerId));
        request.setAttribute("message", messages);
        Agent agent = agentService.selectByregisterId(Integer.valueOf(registerId));
        request.setAttribute("agent", agent);
        List<Message> messageStates = messageService.selectByState(registerId);
        request.setAttribute("messageSize",messageStates.size());

        return "/personal_homepage/broker/intention";
    }

    @PostMapping("/lookmessage")
    public @ResponseBody Result lookById(HttpServletRequest request,Integer id) {
        int count = messageService.updateStateById(id);

        if (count == 1) {
            Result result = new Result("200", "删除成功");
            return result;
        } else {
            Result result = new Result("500", "删除失败");
            return result;
        }
    }


}
